home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / misc / dnstools.shar / hosts-to-rev < prev    next >
Encoding:
Text File  |  1996-10-25  |  2.2 KB  |  120 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # Program to read in the named.hosts file, and generate
  4. # coresponding reverse-lookup files (.inaddr.arpa), one per subnet.
  5. #
  6. # Eric Murray 5/23/93
  7. # ericm@microunity.com
  8.  
  9.  
  10. $hosts = "named.hosts";
  11.  
  12. # list of Class C domains (without the class C part)
  13. # that we want to generate reverse maps for
  14.  
  15. @magic = ( "192.216",);
  16.  
  17. # what's our domain, and who's responsible for this mess?
  18. $domain = "microunity.com.";
  19. $master = "ericm.angst";
  20.  
  21.  
  22. open(FH,$hosts) || die "can't open $hosts";
  23. while (<FH>) {
  24.     chop;
  25.     s/;.*$//;
  26.  
  27.     next if /^$/;
  28.     
  29.     # first, look for the SOA so we can get it for the .rev files..
  30.     if (/SOA/ ) { $in_soa++; next;}
  31.     
  32.     
  33.     if ($in_soa) {
  34.         s/\@//;  # strip the @
  35.         if ($_ =~ /\)/) {
  36.             # end of SOA
  37.             $in_soa = 0;
  38.             $soa .= "\n" . $_ . "\n";
  39.             next;
  40.         }
  41.         else {
  42.             $soa .= "\n" . $_;
  43.             next;
  44.         }
  45.     }
  46.  
  47.     # if we're here, we have the SOA.
  48.     
  49.     local($subnet);
  50.     @a = split;
  51.     if ($a[1] =~ /^A$/ ) {
  52.         # an address
  53.         # but skip localhost:
  54.         next if /^localhost/;
  55.         if (&inmagic($a[2])) {
  56.             @addr = split(/\./,$a[2]);
  57.             $num = $addr[2];
  58.             $name = $a[0];
  59.             # class C!
  60.             $subnet = "$addr[0].$addr[1].$addr[2]";
  61.             if (! $subnets{$subnet}) {
  62.                 # new one.
  63.                 $subnets{$subnet}++;
  64.                 #$subnet = $num;
  65.             }
  66.             #$address{$name} = $addr[3];
  67.             #$names{$subnet} .= $name . " ";
  68.  
  69.             $names{$subnet . "." . $addr[3]} = $name;
  70.             $numbers{$subnet} .= $addr[3] . " ";
  71.         }
  72.     }
  73. }
  74.  
  75. foreach $subnet (keys %subnets) {
  76.     if ($subnet) {
  77.         $file = "named.rev.$subnet";
  78.         open(FH,">$file") || die "can't open $file!\n";
  79.         printf(FH ";\n;\n;  MACHINE GENERATED FILE!\n");
  80.         printf(FH "; DO NOT EDIT!\n;\n\n");
  81.         printf(FH "%s.in-addr.arpa.\tIN\tSOA\t%s\t%s (", &rev($subnet),
  82.              $domain,$master . "." . $domain);
  83.         printf(FH "%s",$soa);
  84.         printf(FH "\n\n");
  85.         @subnetnums = sort(ByNum split(/ /,$numbers{$subnet}));
  86.         foreach $num (@subnetnums){
  87.             printf (FH "%d\t\tIN\tPTR\t%s.%s\n",$num,$names{$subnet . "." . $num},$domain);
  88.             printf (FH "\n");
  89.         }
  90.     }
  91.     close(FH);
  92. }
  93.  
  94. sub ByNum {
  95.     return $a - $b;
  96. }
  97.  
  98. sub inmagic {
  99.     local($add) = @_;
  100.  
  101.     foreach (@magic) {
  102.         return 1 if ($add =~ /^$_/);
  103.     }
  104.     return 0;
  105. }
  106.  
  107.  
  108. sub rev {
  109.     local($in) = @_;
  110.     local($ret);
  111.     # reverse an address:
  112.  
  113.     local(@a) = split('\.',$in);
  114.     foreach (@a) {
  115.         $ret = "$_.$ret";
  116.     }
  117.     $ret =~ s/\.$//;
  118.     return($ret);
  119. }
  120.